In [25]:
import numpy as np
import pandas as pd
import datetime, re

# Getting data from already complied csv file
data = pd.read_csv('bitcoin.csv', encoding='utf-8', parse_dates=True, usecols=["Timestamp", "Close", "Weighted_Price"])

data['Weighted_Price'].fillna(value=0, inplace=True) # for filling NaN with 0 on days where there was no trade
data['Close'].fillna(method='ffill', inplace=True) # to forward propogate the previous values to NaN places

# Turtle Strategy was based on daily data with breakouts in a range of 20-55 days. Therefore we take range to be 55 days.
# As out data are in minutes, convert days into minutes
signal_lookback = 55 * 24 * 60 # days * hours * minutes

data['BuyOrSell'] = np.zeros(len(data)) # initializing the dataframe

data['RollingMax'] = data['Close'].shift(1).rolling(signal_lookback, min_periods=signal_lookback).max()
data['RollingMin'] = data['Close'].shift(1).rolling(signal_lookback, min_periods=signal_lookback).min()
data.loc[data['RollingMax'] < data['Close'], 'BuyOrSell'] = 1
data.loc[data['RollingMin'] > data['Close'], 'BuyOrSell'] = -1

print(data)
# ================ #
# DataFrame TABLE  #
# ================ #
                  Timestamp     Close  Weighted_Price  BuyOrSell  RollingMax  \
0       2017-01-01 00:00:00    967.34      967.372019        0.0         NaN   
1       2017-01-01 00:01:00    967.47      967.415156        0.0         NaN   
2       2017-01-01 00:02:00    966.65      967.333900        0.0         NaN   
3       2017-01-01 00:03:00    967.48      967.357170        0.0         NaN   
4       2017-01-01 00:04:00    966.62      967.479187        0.0         NaN   
5       2017-01-01 00:05:00    967.47      967.477987        0.0         NaN   
6       2017-01-01 00:06:00    966.81      967.315336        0.0         NaN   
7       2017-01-01 00:07:00    967.45      967.440768        0.0         NaN   
8       2017-01-01 00:08:00    967.49      967.370903        0.0         NaN   
9       2017-01-01 00:09:00    967.42      967.427125        0.0         NaN   
10      2017-01-01 00:10:00    967.65      967.551253        0.0         NaN   
11      2017-01-01 00:11:00    967.85      967.848403        0.0         NaN   
12      2017-01-01 00:12:00    967.99      967.994948        0.0         NaN   
13      2017-01-01 00:13:00    967.99      967.477333        0.0         NaN   
14      2017-01-01 00:14:00    968.99      968.447228        0.0         NaN   
15      2017-01-01 00:15:00    968.99      968.917715        0.0         NaN   
16      2017-01-01 00:16:00    968.16      968.976537        0.0         NaN   
17      2017-01-01 00:17:00    969.00      968.997854        0.0         NaN   
18      2017-01-01 00:18:00    968.98      968.787684        0.0         NaN   
19      2017-01-01 00:19:00    968.99      968.987577        0.0         NaN   
20      2017-01-01 00:20:00    968.95      968.303894        0.0         NaN   
21      2017-01-01 00:21:00    968.99      968.770664        0.0         NaN   
22      2017-01-01 00:22:00    968.18      968.299094        0.0         NaN   
23      2017-01-01 00:23:00    968.99      968.876656        0.0         NaN   
24      2017-01-01 00:24:00    968.99      968.936662        0.0         NaN   
25      2017-01-01 00:25:00    968.99      968.986714        0.0         NaN   
26      2017-01-01 00:26:00    969.00      968.985410        0.0         NaN   
27      2017-01-01 00:27:00    968.99      968.980440        0.0         NaN   
28      2017-01-01 00:28:00    968.89      968.961818        0.0         NaN   
29      2017-01-01 00:29:00    968.88      968.867392        0.0         NaN   
...                     ...       ...             ...        ...         ...   
525570  2017-12-31 23:30:00  14000.00    13995.151144        0.0    19891.99   
525571  2017-12-31 23:31:00  14029.33    14019.816053        0.0    19891.99   
525572  2017-12-31 23:32:00  13999.06    14016.228599        0.0    19891.99   
525573  2017-12-31 23:33:00  14001.00    13999.434840        0.0    19891.99   
525574  2017-12-31 23:34:00  14025.00    14017.361960        0.0    19891.99   
525575  2017-12-31 23:35:00  14030.01    14028.969779        0.0    19891.99   
525576  2017-12-31 23:36:00  14042.22    14039.473221        0.0    19891.99   
525577  2017-12-31 23:37:00  14030.01    14037.043940        0.0    19891.99   
525578  2017-12-31 23:38:00  14014.97    14021.304695        0.0    19891.99   
525579  2017-12-31 23:39:00  14004.64    14008.387230        0.0    19891.99   
525580  2017-12-31 23:40:00  14016.02    14015.477877        0.0    19891.99   
525581  2017-12-31 23:41:00  14016.02    14016.017488        0.0    19891.99   
525582  2017-12-31 23:42:00  14016.02    14016.012647        0.0    19891.99   
525583  2017-12-31 23:43:00  14016.05    14011.574794        0.0    19891.99   
525584  2017-12-31 23:44:00  14000.01    14013.720472        0.0    19891.99   
525585  2017-12-31 23:45:00  13970.01    13959.831235        0.0    19891.99   
525586  2017-12-31 23:46:00  13970.01    13975.711839        0.0    19891.99   
525587  2017-12-31 23:47:00  13971.00    13970.903028        0.0    19891.99   
525588  2017-12-31 23:48:00  13979.99    13978.167517        0.0    19891.99   
525589  2017-12-31 23:49:00  13980.00    13979.832748        0.0    19891.99   
525590  2017-12-31 23:50:00  13980.00    13987.401648        0.0    19891.99   
525591  2017-12-31 23:51:00  13987.82    13991.665568        0.0    19891.99   
525592  2017-12-31 23:52:00  13996.64    13992.426441        0.0    19891.99   
525593  2017-12-31 23:53:00  13993.16    13992.179060        0.0    19891.99   
525594  2017-12-31 23:54:00  13993.17    13993.164385        0.0    19891.99   
525595  2017-12-31 23:55:00  13961.23    13978.662816        0.0    19891.99   
525596  2017-12-31 23:56:00  13930.01    13946.754817        0.0    19891.99   
525597  2017-12-31 23:57:00  13930.00    13930.003929        0.0    19891.99   
525598  2017-12-31 23:58:00  13930.01    13930.009965        0.0    19891.99   
525599  2017-12-31 23:59:00  13931.02    13930.015248        0.0    19891.99   

        RollingMin  
0              NaN  
1              NaN  
2              NaN  
3              NaN  
4              NaN  
5              NaN  
6              NaN  
7              NaN  
8              NaN  
9              NaN  
10             NaN  
11             NaN  
12             NaN  
13             NaN  
14             NaN  
15             NaN  
16             NaN  
17             NaN  
18             NaN  
19             NaN  
20             NaN  
21             NaN  
22             NaN  
23             NaN  
24             NaN  
25             NaN  
26             NaN  
27             NaN  
28             NaN  
29             NaN  
...            ...  
525570      5512.0  
525571      5512.0  
525572      5512.0  
525573      5512.0  
525574      5512.0  
525575      5512.0  
525576      5512.0  
525577      5512.0  
525578      5512.0  
525579      5512.0  
525580      5512.0  
525581      5512.0  
525582      5512.0  
525583      5512.0  
525584      5512.0  
525585      5512.0  
525586      5512.0  
525587      5512.0  
525588      5512.0  
525589      5512.0  
525590      5512.0  
525591      5512.0  
525592      5512.0  
525593      5512.0  
525594      5512.0  
525595      5512.0  
525596      5512.0  
525597      5512.0  
525598      5512.0  
525599      5512.0  

[525600 rows x 6 columns]
In [ ]:
import plotly
import plotly.graph_objs as go
from plotly.offline import *
init_notebook_mode(connected=True)

# =================================================== #
# Ploting Closing Price, Rolling Max and Rolling Min  #
# =================================================== #

trace1 = go.Scatter(
    x=data['Timestamp'],
    y=data['Close'],
    mode = 'lines',
    name='Closing Price'
)
trace2 = go.Scatter(
    x=data['Timestamp'],
    y=data['RollingMax'],
    mode = 'lines',
    name='RollingMax'
)
trace3 = go.Scatter(
    x=data['Timestamp'],
    y=data['RollingMin'],
    mode = 'lines',
    name='RollingMin'
)
data = [trace1, trace2, trace3]
plotly.offline.iplot(data, filename='close_rollingMax_Min.html')
In [26]:
# ======================================================================================= #
# Ploting Closing Price and a Subplot showing where to Buy and to Sell (Turtle Strategy)  #
# ======================================================================================= #

trace1 = go.Scatter(
    x=data['Timestamp'],
    y=data['Close']
)
trace2 = go.Scatter(
    x=data['Timestamp'],
    y=data['BuyOrSell']
)
fig = plotly.tools.make_subplots(rows=4, cols=1, specs=[[{'rowspan': 3}], [None], [None], [{}]],
                          shared_xaxes=True, shared_yaxes=True,
                          subplot_titles=('BTC Price','Buy(+1) Or Sell(-1)'),
                          vertical_spacing=0.1)
fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 4, 1)

plotly.offline.iplot(fig, filename='bitcoin_turtle_analysis.html')
This is the format of your plot grid:
[ (1,1) x1,y1 ]
       |       
       |       
[ (4,1) x1,y4 ]